package com.example.bitmaploading.utils;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.example.bitmaploading.BLApplication;

/**
 * get bitmap methods </br>
 * from http://developer.android.com/training/displaying-bitmaps/index.html </br>
 * 
 * @author HAYUKLEUNG
 */
public class ImageUtils
{
	/**
	 * get bitmap from file and make it fit to screen size </br>
	 * 
	 * @param path
	 * @return
	 */
	public static Bitmap getBitmapFitToScreenFromFile(Context context, final String path)
	{
	  BLApplication blApplication = (BLApplication) context.getApplicationContext();
		return decodeSampledBitmapFromFile(path, blApplication.getDisplayWidth(), blApplication.getDisplayHeight());
	}
	
	/**
	 * get bitmap from resource and make it fit to screen size </br>
	 * 
	 * @param res
	 * @param resId
	 * @return
	 */
	public static Bitmap getBitmapFitToScreenFromResource(Context context, Resources res, int resId)
	{
	  BLApplication blApplication = (BLApplication) context.getApplicationContext();
		return decodeSampledBitmapFromResource(res, resId, blApplication.getDisplayWidth(), blApplication.getDisplayHeight());
	}

	/**
	 * get bitmap from resource in sampled decoded </br>
	 * 
	 * @param res
	 * @param resId
	 * @param reqWidth
	 * @param reqHeight
	 * @return
	 */
	public static Bitmap decodeSampledBitmapFromResource(Resources res,
	    int resId, int reqWidth, int reqHeight)
	{
		// First decode with inJustDecodeBounds=true to check dimensions
		final BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeResource(res, resId, options);
		// Calculate inSampleSize
		options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
		// Decode bitmap with inSampleSize set
		options.inJustDecodeBounds = false;
		return BitmapFactory.decodeResource(res, resId, options);
	}

	/**
	 * get bitmap from file in sampled decoded </br>
	 * 
	 * @param path
	 * @param reqWidth
	 * @param reqHeight
	 * @return
	 */
	public static Bitmap decodeSampledBitmapFromFile(final String path,
	    int reqWidth, int reqHeight)
	{
		// First decode with inJustDecodeBounds=true to check dimensions
		final BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(path, options);
		// Calculate inSampleSize
		options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
		// Decode bitmap with inSampleSize set
		options.inJustDecodeBounds = false;
		return BitmapFactory.decodeFile(path, options);
	}

	/**
	 * calculate sample size </br>
	 * 
	 * @param options
	 * @param reqWidth
	 * @param reqHeight
	 * @return
	 */
	private static int calculateInSampleSize(BitmapFactory.Options options,
	    int reqWidth, int reqHeight)
	{
		// Raw height and width of image
		final int height = options.outHeight;
		final int width = options.outWidth;
		int inSampleSize = 1;

		if (height > reqHeight || width > reqWidth)
		{
			// Calculate ratios of height and width to requested height and width
			final int heightRatio = Math.round((float) height / (float) reqHeight);
			final int widthRatio = Math.round((float) width / (float) reqWidth);
			// Choose the smallest ratio as inSampleSize value, this will guarantee
			// a final image with both dimensions larger than or equal to the
			// requested height and width.
			inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
		}
		return inSampleSize;
	}

}
